﻿using System;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;

namespace easyccms.Nhibernate.Infrastructure
{
    /// <summary>
    /// Class that is called by nhibernate to create id convention.(this overrides default one)
    /// </summary>
    public class IdConvention : IIdConvention
    {
        public void Apply(IIdentityInstance identityInstance)
        {
            identityInstance.Column(identityInstance.EntityType.Name + "Id");
        }
    }

    /// <summary>
    /// Class that is called by nhibernate to create foreign key from entity.(this overrides default one)
    /// </summary>
    public class FKConvention : ForeignKeyConvention
    {
        protected override string GetKeyName(FluentNHibernate.Member property, Type type)
        {
            if (property == null)
            {
                // Relationship is many-to-many, one-to-many or join.
                if (type == null)
                    throw new ArgumentNullException("type");

                return type.Name + "Id";
            }

            // Relationship is many-to-one.
            return property.Name + "Id";

        }
    }
}
